package com.example.ovulationperiodtracker.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.ovulationperiodtracker.R;
import com.example.ovulationperiodtracker.databinding.AdapterMedicineBinding;
import com.example.ovulationperiodtracker.databinding.AdapterNotificationdBinding;
import com.example.ovulationperiodtracker.model.MedicineT;
import com.example.ovulationperiodtracker.utils.MyLog;
import com.necer.base.NBaseAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class NotificationDAdapter extends NBaseAdapter<String, AdapterNotificationdBinding> {


    private List<String> allTexts;


    private List<String> checkedList;
    private RecyclerView recyclerView;

    public NotificationDAdapter(Context context, RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
        checkedList = new ArrayList<>();
        allTexts = new ArrayList<>();
        allTexts.add("当天");
        allTexts.add("前1天");
        allTexts.add("前2天");
        allTexts.add("前3天");
        allTexts.add("前4天");
        allTexts.add("前5天");
        allTexts.add("前6天");
        allTexts.add("前7天");

        setData(allTexts);

    }


    @Override
    public AdapterNotificationdBinding createBinding(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        return AdapterNotificationdBinding.inflate(inflater, parent, false);
    }

    @Override
    public void bind(@NonNull AdapterNotificationdBinding binding, @NonNull String item, int position) {
        binding.checkBoxHiddenPillFrag2.setOnCheckedChangeListener(null);
        binding.checkBoxHiddenPillFrag2.setChecked(false);
        binding.checkBoxHiddenPillFrag2.setChecked(checkedList.contains(item));
        binding.txtPillname.setText(item);


        binding.checkBoxHiddenPillFrag2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (checkedList.contains(item)) {
                    checkedList.remove(item);
                } else {
                    checkedList.add(item);
                }

                MyLog.d(" checkedList);" + String.join(",", checkedList));

                recyclerView.post(new Runnable() {
                    @Override
                    public void run() {
                        // 在这里执行数据更新
                        notifyDataSetChanged();
                    }
                });

            }
        });
    }


    public String getChecked() {
        return String.join(",", checkedList);
    }

    public void setChecked(String checkedMedicines) {
        if (TextUtils.isEmpty(checkedMedicines)) {
            return;
        }
        this.checkedList = new ArrayList<>(Arrays.asList(checkedMedicines.split(",")));
    }
}
